<?
/* The Connector classes define database connection objects.
 * The abstract class Connector can be extended to support different database drivers (mysql, postgresql, etc...)
 *
 */
abstract class Connector{
    public function  __construct($host, $user, $pw, $db_name) {
        $this->connect($host, $user, $pw, $db_name);
    }

    private function connect($host, $user, $pw, $db_name){
        if(!$this->dbConnect($host, $user, $pw)) {
            error_log("ERROR===" . $this->dbGetError());
        }else{
            return $this->dbSelect($db_name);
        }

        return false;
    }

    protected function logError($query){
        $error = $this->dbGetError();
        trigger_error($error.":".$query, E_USER_ERROR);
    }

    public function query($query){
        if($result = $this->dbQuery($query)){
            return $result;
        }else{
            $this->logError($query);
        }
        return false;
    }

    abstract protected function dbGetError();
    abstract protected function dbQuery($query);
    abstract protected function dbConnect($db_host, $db_user, $db_pw);
    abstract protected function dbSelect($db_name);
}

class MySQLConnector extends Connector {
    public function  __construct($host, $user, $pw, $db_name) {
        parent::__construct($host, $user, $pw, $db_name);
    }

    protected function dbGetError(){
        return mysql_error();
    }

    protected function dbQuery($query){
        return mysql_query($query);
    }

    protected function dbConnect($db_host, $db_user, $db_pw){
        return mysql_connect($db_host, $db_user, $db_pw);
    }

    protected function dbSelect($db_name){
        return mysql_select_db($db_name);
    }
}
?>